<template>
  <div id="dictGroupComponent">
    <div>
      <div class="filter-container">
        <!-- <div class="filter-item">
          <span class="span">实体名称 </span>
          <el-input v-model="listQuery.keyword" style="width: 200px;" placeholder="请输入分组名称或编码"
            @keyup.enter.native="handleFilter" />
        </div>
        <el-button v-permission="['sys:dict:group:list']" v-waves class="filter-item" type="primary" icon="el-icon-search"
          @click="handleFilter">{{ $t('table.search') }}</el-button> -->
        <el-button v-permission="['sys:dict:group:add']" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
      </div>

      <el-table :key="tableKey" v-loading="listLoading" :data="entityeData" element-loading-text="正在加载，请稍后" border fit highlight-current-row style="width: 100%" @row-click="chosehDicts">
        <el-table-column label="实体名称" min-width="100px">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column :label="$t('table.actions')" align="center" width="180" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-permission="['sys:dict:group:update']" size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
            <el-button v-permission="['sys:dict:group:delete']" size="small" type="text" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(scope.row)">{{ $t('table.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination :current-page.sync="listQuery.page" :page-sizes="pageArray" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>

    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :append-to-body="true" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 90%; margin-left:50px;">
        <el-row :gutter="40">
          <el-col :span="24">
            <el-form-item label="实体名称" prop="name">
              <el-input v-model.trim="temp.name" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogStatus=='create'?create():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { fetchDictGroupList, createDictGroup, deleteDictGroup, updateDictGroup } from '@/api/sys/dictGroup'
import { labelEntityList, entityTable, addEntiyt, updateEntiyt, deleteEntiyt } from '@/api/metadata/entity'
import permission from '@/directive/permission/permission'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'SysDictGroupComponent',
  props: ['entityeData', 'tableId'],
  directives: {
    waves,
    permission,
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.getters.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined,
      },
      showReviewer: false,
      temp: {
        labelGroup: '',
        name: '',
        tableId: '',
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑实体',
        create: '添加实体',
      },
      rules: {
        name: [{ required: true, message: '实体名称必填', trigger: 'change' }],
        code: [{ required: true, message: '分组编码必填', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.total = this.entityeData ? this.entityeData.length : 0
  },
  updated() {
    this.total = this.entityeData ? this.entityeData.length : 0
  },
  methods: {
    create() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.tableId = this.tableId
          addEntiyt(this.temp).then((res) => {
            if (res.data.code == 0) {
              this.$message.success(res.data.msg)
              this.$emit('refreshDicts')
            } else {
              this.$message.error(res.data.msg)
            }
            this.dialogFormVisible = false
          })
        }
      })
    },
    chosehDicts(row) {
      this.$emit('passDict', row)
    },
    getList() {
      this.listLoading = true
      fetchDictGroupList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success',
      })
      row.status = status
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        name: '',
        code: '',
        remarks: '',
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createDictGroup(this.temp).then((response) => {
            this.loading = false
            const data = response.data
            if (data.code === 0) {
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.getList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = {
        labelGroup: row.labelGroup ? row.labelGroup : [],
        name: row.name,
        tableId: row.tableId,
        id: row.id,
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateEntiyt(this.temp).then((res) => {
            if (res.data.code == 0) {
              this.dialogFormVisible = false
              this.$emit('refreshDicts')
              this.$message.success(res.data.msg)
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteEntiyt({ ids: row.id }).then((response) => {
          const data = response.data
          if (data.code === 0) {
            this.$message.success('删除成功')
            this.$emit('refreshDicts')
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
  },
}
</script>
<style>
.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
